package com.binance.mbxgateway.web.service.impl;

import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.binance.master.utils.StringUtils;
import com.binance.master.utils.WebUtils;
import com.binance.mbxgateway.vo.request.info.GetUserUrlReqeust;
import com.binance.mbxgateway.web.service.IInfoService;
import com.binance.mbxgateway.web.utils.Constants;

@Service
public class InfoServiceImpl extends BaseServiceImpl implements IInfoService {

    private static final AtomicInteger WSS_URL_INDEX = new AtomicInteger(0);
    private static final AtomicInteger KLINE_URL_INDEX = new AtomicInteger(0);
    private static final AtomicInteger MKTDATA_URL_INDEX = new AtomicInteger(0);
    private static final AtomicInteger USER_URL_INDEX = new AtomicInteger(0);
    private static final AtomicInteger DEPTH_URL_INDEX = new AtomicInteger(0);

    @Value("${matchbox.rest.root.url}")
    private String restUrl;
    @Value("${matchbox.wss.public.root.url}")
    private String wssUrl;
    @Value("${matching.unit.type}")
    private String matchingType;
    @Value("${bijie.branding}")
    private Boolean branding;
    private String[] wssUrlList;
    @Value("${matchbox.depth.public.root.url}")
    private String depthUrl;
    @Value("${matchbox.kline.public.root.url}")
    private String klineUrl;
    private String[] klineUrlList;
    @Value("${matchbox.user.internal.root.url}")
    private String userUrl;
    @Value("${matchbox.user.public.root.url}")
    private String publicUserUrl;
    @Value("${mktdata.wss.url}")
    private String mktdataWssUrl;
    private String[] mktdataUrlList;

    @PostConstruct
    public void init() {
        wssUrlList = wssUrl.split(",");
        klineUrlList = klineUrl.split(",");
        mktdataUrlList = mktdataWssUrl.split(",");
    }

    @Override
    public String getWssUrl(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        if (StringUtils.isNoneBlank(url) && (url.contains("binance.cloud") || url.contains("binance.co/"))) {
            return Constants.DEFAULT_WSS;
        }
        int i = WSS_URL_INDEX.getAndIncrement() % wssUrlList.length;
        if (i < 0)
            i = i + wssUrlList.length;
        return wssUrlList[i];
    }

    @Override
    public String getKlineUrl(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        if (StringUtils.isNoneBlank(url) && (url.contains("binance.cloud") || url.contains("binance.co/"))) {
            return Constants.DEFAULT_WSS;
        }
        int i = KLINE_URL_INDEX.getAndIncrement() % klineUrlList.length;
        if (i < 0)
            i = i + klineUrlList.length;
        return klineUrlList[i];
    }

    @Override
    public String getMktdataWssUrl(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        if (StringUtils.isNoneBlank(url) && (url.contains("binance.cloud") || url.contains("binance.co/"))) {
            return Constants.DEFAULT_WSS;
        }
        int i = MKTDATA_URL_INDEX.getAndIncrement() % mktdataUrlList.length;
        if (i < 0)
            i = i + mktdataUrlList.length;
        return mktdataUrlList[i];
    }

    @Override
    public Long getServerTime() {
        return System.currentTimeMillis();
    }

    @Override
    public Boolean getBranding() {
        return branding;
    }

    @Override
    public String getUserUrl(GetUserUrlReqeust body) {
        if (body.getBackend().booleanValue()) {
            return this.userUrl;
        } else {
            String url = WebUtils.getHttpServletRequest().getRequestURL().toString();
            if (StringUtils.isNotBlank(url) && (url.contains("binance.cloud") || url.contains("binance.co/"))) {
                return "wss://stream2.binance.cloud:443/ws";
            }
            String[] userUrlList = StringUtils.split(publicUserUrl, ",");
            int i = USER_URL_INDEX.getAndIncrement() % userUrlList.length;
            if (i < 0)
                i = i + userUrlList.length;
            return userUrlList[i];
        }
    }

    @Override
    public String getDepthUrl() {
        String url = WebUtils.getHttpServletRequest().getRequestURL().toString();
        if (StringUtils.isNoneBlank(url) && (url.contains("binance.cloud") || url.contains("binance.co/"))) {
            return "wss://stream2.binance.cloud:443/ws";
        }
        String[] depthUrlList = StringUtils.split(this.depthUrl, ",");
        int i = DEPTH_URL_INDEX.getAndIncrement() % depthUrlList.length;
        if (i < 0)
            i = i + depthUrlList.length;
        return depthUrlList[i];
    }

}
